<?php

/**
 * Add body classes if certain regions have content.
 */
function technews_preprocess_html(&$variables)
{
    if (!empty($variables['page']['featured'])) {
        $variables['classes_array'][] = 'featured';
    }

    if (!empty($variables['page']['triptych_first'])
        || !empty($variables['page']['triptych_middle'])
        || !empty($variables['page']['triptych_last'])
    ) {
        $variables['classes_array'][] = 'triptych';
    }

    if (!empty($variables['page']['footer_firstcolumn'])
        || !empty($variables['page']['footer_secondcolumn'])
        || !empty($variables['page']['footer_thirdcolumn'])
        || !empty($variables['page']['footer_fourthcolumn'])
    ) {
        $variables['classes_array'][] = 'footer-columns';
    }
	

    // Add conditional stylesheets for IE
    drupal_add_css(path_to_theme() . '/css/ie.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'lte IE 7', '!IE' => FALSE), 'preprocess' => FALSE));
    drupal_add_css(path_to_theme() . '/css/ie6.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'IE 6', '!IE' => FALSE), 'preprocess' => FALSE));
}

/**
 * Override or insert variables into the page template for HTML output.
 */
function technews_process_html(&$variables)
{
    // Hook into color.module.
    if (module_exists('color')) {
        _color_html_alter($variables);
    }
}

/**
 * Override or insert variables into the page template.
 */
function technews_process_page(&$variables)
{
    // Hook into color.module.
    if (module_exists('color')) {
        _color_page_alter($variables);
    }
    // Always print the site name and slogan, but if they are toggled off, we'll
    // just hide them visually.
    $variables['hide_site_name'] = theme_get_setting('toggle_name') ? FALSE : TRUE;
    $variables['hide_site_slogan'] = theme_get_setting('toggle_slogan') ? FALSE : TRUE;
    if ($variables['hide_site_name']) {
        // If toggle_name is FALSE, the site_name will be empty, so we rebuild it.
        $variables['site_name'] = filter_xss_admin(variable_get('site_name', 'Drupal'));
    }
    if ($variables['hide_site_slogan']) {
        // If toggle_site_slogan is FALSE, the site_slogan will be empty, so we rebuild it.
        $variables['site_slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
    }
    // Since the title and the shortcut link are both block level elements,
    // positioning them next to each other is much simpler with a wrapper div.
    if (!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']) {
        // Add a wrapper div using the title_prefix and title_suffix render elements.
        $variables['title_prefix']['shortcut_wrapper'] = array(
            '#markup' => '<div class="shortcut-wrapper clearfix">',
            '#weight' => 100,
        );
        $variables['title_suffix']['shortcut_wrapper'] = array(
            '#markup' => '</div>',
            '#weight' => -99,
        );
        // Make sure the shortcut link is the first item in title_suffix.
        $variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
    }
}

/**
 * Implements hook_preprocess_maintenance_page().
 */
function technews_preprocess_maintenance_page(&$variables)
{
    // By default, site_name is set to Drupal if no db connection is available
    // or during site installation. Setting site_name to an empty string makes
    // the site and update pages look cleaner.
    // @see template_preprocess_maintenance_page
    if (!$variables['db_is_active']) {
        $variables['site_name'] = '';
    }
    drupal_add_css(drupal_get_path('theme', 'technews') . '/css/maintenance-page.css');
}

/**
 * Override or insert variables into the maintenance page template.
 */
function technews_process_maintenance_page(&$variables)
{
    // Always print the site name and slogan, but if they are toggled off, we'll
    // just hide them visually.
    $variables['hide_site_name'] = theme_get_setting('toggle_name') ? FALSE : TRUE;
    $variables['hide_site_slogan'] = theme_get_setting('toggle_slogan') ? FALSE : TRUE;
    if ($variables['hide_site_name']) {
        // If toggle_name is FALSE, the site_name will be empty, so we rebuild it.
        $variables['site_name'] = filter_xss_admin(variable_get('site_name', 'Drupal'));
    }
    if ($variables['hide_site_slogan']) {
        // If toggle_site_slogan is FALSE, the site_slogan will be empty, so we rebuild it.
        $variables['site_slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
    }
}

/**
 * Override or insert variables into the node template.
 */
function technews_preprocess_node(&$variables)
{
    if ($variables['view_mode'] == 'full' && node_is_page($variables['node'])) {
        $variables['classes_array'][] = 'node-full';
    }

}

/**
 * Override or insert variables into the block template.
 */
function technews_preprocess_block(&$variables)
{
    // In the header region visually hide block titles.
    if ($variables['block']->region == 'header') {
        $variables['title_attributes_array']['class'][] = 'element-invisible';
    }
}

/**
 * Implements theme_menu_tree().
 */
function technews_menu_tree($variables)
{
    return '<ul class="menu clearfix">' . $variables['tree'] . '</ul>';
}

/**
 * Implements theme_field__field_type().
 */
function technews_field__taxonomy_term_reference($variables)
{
    $output = '';

    // Render the label, if it's not hidden.
    if (!$variables['label_hidden']) {
        $output .= '<h3 class="field-label">' . $variables['label'] . ': </h3>';
    }

    // Render the items.
    $output .= ($variables['element']['#label_display'] == 'inline') ? '<ul class="links inline">' : '<ul class="links">';
    foreach ($variables['items'] as $delta => $item) {
        $output .= '<li class="taxonomy-term-reference-' . $delta . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</li>';
    }
    $output .= '</ul>';

    // Render the top-level DIV.
    $output = '<div class="' . $variables['classes'] . (!in_array('clearfix', $variables['classes_array']) ? ' clearfix' : '') . '"' . $variables['attributes'] . '>' . $output . '</div>';

    return $output;
}

function technews_preprocess_page(&$variables)
{
    $search_box = drupal_render(drupal_get_form('search_form'));
    $variables['search_box'] = $search_box;
    $status = drupal_get_http_header("status");
    if ($status == "404 Not Found") {
        $variables['theme_hook_suggestions'][] = 'page__404';
    }
}

function phptemplate_views_view_list_MonthlyArchive($view, $nodes, $type)
{
    $fields = _views_get_fields();

    $taken = array();

    // Set up the fields in nicely named chunks.
    foreach ($view->field as $id => $field) {
        $field_name = $field['field'];
        if (isset($taken[$field_name])) {
            $field_name = $field['queryname'];
        }
        $taken[$field_name] = true;
        $field_names[$id] = $field_name;
    }

    // Set up some variables that won't change.
    $base_vars = array(
        'view' => $view,
        'view_type' => $type,
    );

    foreach ($nodes as $i => $node) {
        $vars = $base_vars;
        $vars['node'] = $node;
        $vars['count'] = $i;
        $vars['stripe'] = $i % 2 ? 'even' : 'odd';
        foreach ($view->field as $id => $field) {
            $name = $field_names[$id];
            $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
            if (isset($field['label'])) {
                $vars[$name . '_label'] = $field['label'];
            }
        }
        $items[] = _phptemplate_callback('views-list-MonthlyArchive', $vars);
    }
    if ($items) {
        return theme('item_list', $items);
    }
}

function hook_form_FORM_ID_alter(&$form, &$form_state, $form_id)
{
    // Modification for the form with the given form ID goes here. For example, if
    // FORM_ID is "user_register_form" this code would run only on the user
    // registration form.

    // Add a checkbox to registration form about agreeing to terms of use.
    $form['terms_of_use'] = array(
        '#type' => 'checkbox',
        '#title' => t("I agree with the website's terms and conditions."),
        '#required' => TRUE,
    );
}

function MODULE_menu()
{
    $items['page-not-found'] = array(
        'title' => '',
        'page callback' => 'MODULE_page_not_found',
        'access callback' => TRUE,
    );
    return $items;
}

function MODULE_page_not_found()
{
    drupal_set_title('Page not found');
    $cust_err = "";
    $cust_err = $cust_err . "The requested page " . current_path() . " could not be found";
    return $cust_err;
}

function technews_comment_block()
{
    global $theme_key;
    $output = '<div class="content"><table><tbody>';
    $number = variable_get('comment_block_count', 5);
    $i = 0;
    foreach (comment_get_recent($number) as $comment) {

        $char = $i%2==0?"odd":"even";
        // if core include it, it would be performance gain instead of calling comment_load again
        $comment_object = comment_load($comment->cid);
        !empty($comment_object->picture) && file_exists($comment_object->picture) ? $picture = $comment_object->picture : $picture = variable_get('user_picture_default', drupal_get_path('theme', $theme_key) . '/css/images/guest/anonymous50.png');
        $themed_picture = theme('image', array('path' => $picture, 'alt' => $comment_object->name, 'title' => $comment_object->name, 'width' => '60px', 'height' => '60px'));
        $output .= '<tr class="'.$char.'"><td class="title-author">' . l($themed_picture, 'user/' . $comment_object->u_uid, array('html' => TRUE)) . '';
        $output .= '<div class="node-title">' . $comment_object->name . ' ' . t('say') . ':';
        $output .= '<div class="time-ago">' . truncate_utf8($comment_object->comment_body[$comment_object->language][0]['value'], 50, $wordsafe = TRUE, $dots = TRUE) . '</div></div></td>';
        $i++;
    }
    $output .='</tr></tbody></table></div>';
    if (!empty($output)) {
        return $output;
    } else {
        return t('No comments available.');
    }
}

function technews_node_recent_content($variables)
{
    $node = $variables['node'];
//    !empty($comment_object->picture) && file_exists($comment_object->picture) ? $picture = $comment_object->picture : $picture = variable_get('user_picture_default', drupal_get_path('theme', $theme_key) . '/css/images/guest/anonymous50.png');
    $themed_picture = theme('image', array('path' => '/sites/default/files/styles/thumb_img/public/field/image/'.$node->field_image['und'][0]['filename'], 'alt' => $comment_object->name, 'title' => $comment_object->name, 'width' => '60px', 'height' => '60px'));
    $output = '' . l($themed_picture, 'user/' . $comment_object->u_uid, array('html' => TRUE)) . '';
    $output .= '<div class="node-title">';
    $output .= l($node->title, 'node/' . $node->nid);
    $output .= '<div class="time-ago">' . t('@time ago', array('@time' => format_interval(time() - $node->created))) . '</div>';
    $output .= theme('mark', array('type' => node_mark($node->nid, $node->changed)));
    $output .= '</div>';

    return $output;
}

?>
